package org.example.gradleadmin;

import java.util.*;

public class PermissionResolver {

    static class Permission {
        int id;
        int parentId;
        String parentIds;
        String permCode;

        public Permission(int id, int parentId, String parentIds, String permCode) {
            this.id = id;
            this.parentId = parentId;
            this.parentIds = parentIds;
            this.permCode = permCode;
        }

        @Override
        public String toString() {
            return "Permission{id=" + id + ", permCode='" + permCode + "'}";
        }
    }

    /**
     * 给定权限列表和角色拥有的权限 ID，返回最终权限集合（包含子权限）
     */
    public static Set<Permission> getAllInheritedPermissions(List<Permission> allPermissions, Set<Integer> rolePermissionIds) {
        // 1. 构建 parentId -> children 映射
        Map<Integer, List<Permission>> parentChildrenMap = new HashMap<>();
        Map<Integer, Permission> idPermissionMap = new HashMap<>();
        for (Permission perm : allPermissions) {
            parentChildrenMap
                    .computeIfAbsent(perm.parentId, k -> new ArrayList<>())
                    .add(perm);
            idPermissionMap.put(perm.id, perm);
        }

        // 2. 收集结果（防止重复）
        Set<Integer> visitedIds = new HashSet<>();
        Set<Permission> result = new HashSet<>();

        for (Integer id : rolePermissionIds) {
            collectChildren(id, parentChildrenMap, idPermissionMap, visitedIds, result);
        }

        return result;
    }

    /**
     * 递归查找所有子权限
     */
    private static void collectChildren(int id,
                                        Map<Integer, List<Permission>> parentChildrenMap,
                                        Map<Integer, Permission> idPermissionMap,
                                        Set<Integer> visited,
                                        Set<Permission> result) {
        if (!visited.add(id)) return; // 避免重复或死循环

        Permission perm = idPermissionMap.get(id);
        if (perm != null) {
            result.add(perm);
        }

        List<Permission> children = parentChildrenMap.getOrDefault(id, Collections.emptyList());
        for (Permission child : children) {
            collectChildren(child.id, parentChildrenMap, idPermissionMap, visited, result);
        }
    }

    // 测试
    public static void main(String[] args) {
        List<Permission> list = new ArrayList<>();
        list.add(new Permission(1, -1, "-1", "用户列表-查看"));
        list.add(new Permission(2, -1, "-1", "用户列表-管理"));
        list.add(new Permission(3, 2, "-1,2", "用户列表-新增"));
        list.add(new Permission(4, 2, "-1,2", "用户列表-修改"));
        list.add(new Permission(5, 3, "-1,2,3", "用户列表-导出"));
        list.add(new Permission(6, 5, "-1,2,3,5", "用户列表-批量导出"));

        System.out.println("== 角色拥有权限 ID = 2 ==");
        Set<Permission> perms1 = getAllInheritedPermissions(list, Set.of(2,3,4));
        perms1.forEach(System.out::println);

        System.out.println("\n== 角色拥有权限 ID = 6 ==");
        Set<Permission> perms2 = getAllInheritedPermissions(list, Set.of(6));
        perms2.forEach(System.out::println);
    }
}
